package com.hxkcloud.patient.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

/**
 * 一些有关于屏幕尺寸获取，尺寸单位转换等方法
 * @author meeno.yh
 *
 */
public class ScreenUtils 
{
	private static DisplayMetrics dm;
	/**
	 * 获取DisplayMetrics对象并处理
	 */
	private static void init(Activity activity)
	{
		if (dm == null) 
		{
			dm = new DisplayMetrics();
			activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		}
	}
	/**
	 * 获得状态栏的高度
	 *
	 * @param context
	 * @return
	 */
	public static int getStatusHeight(Context context) {

		int statusHeight = -1;
		try {
			Class clazz = Class.forName("com.android.internal.R$dimen");
			Object object = clazz.newInstance();
			int height = Integer.parseInt(clazz.getField("status_bar_height")
					.get(object).toString());
			statusHeight = context.getResources().getDimensionPixelSize(height);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return statusHeight;
	}
	/**
	 * 获取屏幕的宽高
	 * activity:
	 */
	public static int[] getScreenSize(Activity activity)
	{
		int [] a = new int[2] ;
		init(activity);
		a[0] = dm.widthPixels;
		a[1] = dm.heightPixels;
		return a;
	}
	
	/**
	 * dip转换成px
	 */
	public static int dipToPx(Activity activity, int dip)
	{
		int px = 0;
		init(activity);
		float density = dm.density;
		px = (int)(dip*density+0.5f);
		return px;
	}
	/**
	 * px转换成dip
	 * 
	 */
	public static int pxToDip(Activity activity, int px)
	{
		int dip = 0;
		init(activity);
		float density = dm.density;
		dip = (int)(px/density+0.5f);
		return dip;
	}
	/**
	 * sp转换成px
	 */
	public static int spToPx(Activity activity, int sp)
	{
		int px = 0;
		init(activity);
		float scaledDensity = dm.scaledDensity;
		px = (int)(sp*scaledDensity + 0.5f);
		return px;
	}
	
	/**
	 * px转换成sp
	 */
	public static int pxToSp(Activity activity, int px)
	{
		int sp = 0;
		init(activity);
		float scaleDensity = dm.scaledDensity;
		sp = (int)(sp/scaleDensity +0.5f);
		return sp;
	}
}
